package com.briup.smartcity_group1.web.controller.auth;

import com.briup.smartcity_group1.bean.basic.Carousel;
import com.briup.smartcity_group1.consts.BeanNameConsts;
import com.briup.smartcity_group1.consts.DataMessageConsts;
import com.briup.smartcity_group1.service.ICarouselService;
import com.briup.smartcity_group1.utils.result.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图相关操作控制器
 *
 * @author WangChen
 * @date 2023/1/5 9:08
 */
@RestController
@RequestMapping(BeanNameConsts.CAROUSEL)
public class CarouselController {

    @Autowired
    private ICarouselService service;

    @DeleteMapping
    public void deleteById(@RequestParam(required = true) Integer id){
        service.deleteById(id);
        Result.setDataMessage(DataMessageConsts.CAROUSEL_DELETE_SUCCESS);
    }

    @GetMapping(value = "/yes")
    public List<Carousel> getAllByYes(){
        return service.findYesCarousel();
    }

    @GetMapping(value = "/page")
    public PageInfo<Carousel> getAllByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "2") Integer pageSize){
        return service.findAllByPage(pageNum,pageSize);
    }

    @PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void save(@RequestParam(required = true) String carouselInfo,
                     @RequestParam(required = true) String carouselName,
                     @RequestParam(required = true) String carouselUrl){
        Carousel carousel = new Carousel();
        carousel.setCarouselInfo(carouselInfo);
        carousel.setCarouselName(carouselName);
        carousel.setCarouselUrl(carouselUrl);

        service.saveCarousel(carousel);
        Result.setDataMessage(DataMessageConsts.CAROUSEL_CREATE_SUCCESS);
    }

    @PutMapping
    public void update(@RequestParam(required = true) Integer carouselId,
                       @RequestParam(required = false) String carouselInfo,
                       @RequestParam(required = false) String carouselName,
                       @RequestParam(required = false) Integer carouselStatus,
                       @RequestParam(required = false) String carouselUrl){

//        Carousel carousel = Carousel.builder()
//                .carouselId(carouselId)
//                .carouselInfo(carouselInfo)
//                .carouselName(carouselName)
//                .carouselStatus(carouselStatus)
//                .carouselUrl(carouselUrl)
//                .build();

        Carousel carousel = new Carousel();
        carousel.setCarouselId(carouselId);
        carousel.setCarouselInfo(carouselInfo);
        carousel.setCarouselName(carouselName);
        carousel.setCarouselStatus(carouselStatus);
        carousel.setCarouselUrl(carouselUrl);

        service.updateCarousel(carousel);
        Result.setDataMessage(DataMessageConsts.CAROUSEL_UPDATE_SUCCESS);
    }

}
